### Replication file for
# Title: From Foe to Friend? Government-Opposition Conflict and the Appointment of Cabinet Ministers
# Authors: Herzog, Alexander (alexander.herzog@uni-bamberg.de ; University of Bamberg); Schmuck, David (david.schmuck@uni-bamberg.de ; University of Bamberg , corresponding author)
# Journal: Political Science Research and Methods

# APPENDIX D Debate Weights

# Pre-Settings ----
# Set working directory
# Note: Set working directory for log and input file accordingly
setwd("")

# Clear environment
rm(list = ls(all = TRUE))

# Install and load packages
library(tidyverse) # for data manipulation

# Load data file
load("Replication data - PSRM - Herzog Schmuck - From Foe to Friend - Appendix D Debate Weights.RData")

# Summarize betas by debate type ----
betas_by_debate_what <- debates |>
  group_by(debate_what) |> 
  summarize(mean   = mean(abs_beta),
            median = median(abs_beta),
            sd     = sd(abs_beta),
            min    = min(abs_beta),
            max    = max(abs_beta)) |> 
  arrange(desc(mean))
betas_by_debate_what

# Generate better debate labels ----
debates <- debates |> 
  mutate(debate_labels = case_when(
    debate_what=="briefing" ~ "Briefing",
    debate_what=="budget" ~ "Budget Debate",
    debate_what=="consultation" ~ "Consultation",
    debate_what=="current_affairs" ~ "Current Affairs",
    debate_what=="debate" ~ "Debate",
    debate_what=="election" ~ "Election",
    debate_what=="government_declaration" ~ "Government Declaration",
    debate_what=="missing" ~ "(Missing)",
    debate_what=="mixed_labels" ~ "(Mixed Labels)",         
    debate_what=="motion" ~ "Motion",    
    debate_what=="questions" ~ "Questions",          
    debate_what=="report" ~ "Report",
    debate_what=="section" ~ "Section"
  )) 

# Reorder by median value of abs_beta
debates <- debates |> 
  mutate(debate_labels = fct_reorder(debate_labels, abs_beta, .fun = median, .desc = TRUE))

# Figure D.3: Distribution of absolute debate weights (\beta_j) by GermaParl debate labels 
figD3 <- ggplot(data = debates, aes(x = debate_labels, y = abs_beta)) +
  theme_bw(base_size = 18) +
  geom_boxplot() +
  #geom_violin(width=1) +
  #geom_boxplot(width=0.1, color="black", alpha=0.2) +
  xlab("GermaParl Debate Label") +
  ylab("Absolute Debate Weight") +
  scale_y_continuous(breaks = seq(0, 1, 0.1)) +
  theme(text=element_text(size=16),
        axis.text.x = element_text(angle = 45, hjust=1)) 

# Plot the graph
plot(figD3)

# Save plot
ggsave(
  filename = "Herzog_Schmuck_FigD3_Debate_weights.tiff",
  device='tiff',
  dpi = 200,
  width = 11,
  height = 9.89,
  units = "in",
  compression = "lzw"
)
